import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'dart:async';

main(List<String> args) {
  test_serversocket();
}

void test_future() {
  HttpServer.bind("127.0.0.1", 4444).then((server) {
    print("isBroadcast: ${server.isBroadcast}");
    server.close();
  }).catchError(print);
  return;
}

void test_serversocket() async {
  var socket = await ServerSocket.bind("127.0.0.1", 8888);
  socket.listen((client) {
    print("Client IP: ${client.address}");
    var file = File("test.txt");
    var sink = file.openWrite(mode: FileMode.append);

    client.listen((event) {
      sink.write(utf8.decode(event));
    });
    // client.add(utf8.enclsode("Hello world"));
    client.close();
    // sink.close();
  });
}

void test_stream() {
  Stream<List<int>> stream = File("config.txt").openRead();
  stream.transform(utf8.decoder).listen(print);
}

void test_math() {
  print("e: $e\n"
      "ln2: $ln2\n"
      "ln10: $ln10\n"
      "log2e: $log2e\n"
      "log10e: $log10e\n"
      "pi: $pi\n"
      "sqrt1_2: $sqrt1_2\n"
      "sqrt2: $sqrt2"
      "cos(45): ${cos(60 * pi / 180)}\n"
      "max(39, 45): ${max(39, 45)}");
}
